package com.example.overdue.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.fresh.R;
import com.example.overdue.MainActivity2;
import com.example.overdue.data.OberdueData;
import com.example.overdue.util.DataUtil;

import java.util.List;

//月份的列表适配器
public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.MyHolder> {

    private List<OberdueData> mList;//数据源
    private String year = "";//选择的年份
    private Context mContext;

    public MyRecycleViewAdapter(List<OberdueData> list, String year) {
        mList = list;
        this.year = year;
    }

    //创建ViewHolder并返回，后续item布局里控件都是从ViewHolder中取出
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //将自定义的item布局R.layout.item_one转换为View
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_layout, parent, false);
        //将view传递给我们自定义的ViewHolder
        MyHolder holder = new MyHolder(view);
        mContext = parent.getContext();
        //返回这个MyHolder实体
        return holder;
    }

    //通过方法提供的ViewHolder，将数据绑定到ViewHolder中
    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        if (position == mList.size() - 1) {//如果是最后一个条目，隐藏掉竖线
            holder.item_shuxian.setVisibility(View.GONE);
        } else {
            holder.item_shuxian.setVisibility(View.VISIBLE);
        }

        if (position < 9) {
            holder.item_day.setText("0" + (position + 1) + "月");
        } else {
            holder.item_day.setText((position + 1) + "月");
        }
        //获取月份
        String month = "";
        if (position < 9) {
            month = "0" + (position + 1);
        } else {
            month = "" + (position + 1);
        }
        int monthOberdueNum = 0;//当前年月过期的物品数量
        for (int i = 0; i < mList.size(); i++) {

            if (DataUtil.showTime(mList.get(i).getTime()).contains(year + "年" + month + "月")) {
                monthOberdueNum++;
            }
        }
        if (monthOberdueNum == 0) {
            holder.item_content.setText("没有过期物品");
            holder.item_content.setTextColor(Color.parseColor("#999999"));
        } else {
            holder.item_content.setText(monthOberdueNum + "个物品过期");
            holder.item_content.setTextColor(Color.parseColor("#6200EE"));
        }

        //把月份给控件，用于点击事件的时候获取
        holder.item_day.setTag(month);
        //设置月份的点击事件
        holder.item_day.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取月份
                String month = (String) v.getTag();
                Intent intent=new Intent(mContext, MainActivity2.class);
                intent.putExtra("year",year);
                intent.putExtra("month",month);
                mContext.startActivity(intent);
            }
        });

    }

    //获取数据源总的条数
    @Override
    public int getItemCount() {
        return 12;
    }

    /**
     * 自定义的ViewHolder
     */
    class MyHolder extends RecyclerView.ViewHolder {

        private final ImageView item_shuxian;
        private final TextView item_day;
        private final TextView item_content;

        public MyHolder(View itemView) {
            super(itemView);
            //找id
            item_shuxian = itemView.findViewById(R.id.item_shuxian);
            item_day = itemView.findViewById(R.id.item_day);
            item_content = itemView.findViewById(R.id.item_content);
        }
    }
}



